package com.edu.bhu.admin.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.edu.bhu.admin.domain.LibBook;
import com.edu.bhu.admin.mapper.LibBookMapper;
import com.edu.bhu.admin.util.loanStatusUtil;
import com.edu.bhu.common.core.domain.entity.SysUser;
import com.edu.bhu.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.edu.bhu.admin.mapper.LibLoanMapper;
import com.edu.bhu.admin.domain.LibLoan;
import com.edu.bhu.admin.service.ILibLoanService;
import com.edu.bhu.common.core.text.Convert;

/**
 * 借阅信息Service业务层处理
 * 
 * @author wangweibo
 * @date 2022-02-14
 */
@Service
public class LibLoanServiceImpl implements ILibLoanService 
{
    @Autowired
    private LibLoanMapper libLoanMapper;

    @Autowired
    private LibBookMapper libBookMapper;

    /**
     * 查询借阅信息
     * 
     * @param loanId 借阅信息主键
     * @return 借阅信息
     */
    @Override
    public LibLoan selectLibLoanByLoanId(Long loanId)
    {
        return libLoanMapper.selectLibLoanByLoanId(loanId);
    }

    /**
     * 查询借阅信息列表
     * 
     * @param libLoan 借阅信息
     * @return 借阅信息
     */
    @Override
    public List<LibLoan> selectLibLoanList(LibLoan libLoan)
    {
        return libLoanMapper.selectLibLoanList(libLoan);
    }

    /**
     * 查询借阅信息列表(关联用户和书本信息)
     *
     * @param libLoan 借阅信息
     * @param libBook 书本信息
     * @param sysUser 用户信息
     * @return
     */
    public List<LibLoan> selectLibLoanWithInfoList(LibLoan libLoan, LibBook libBook, SysUser sysUser)
    {
        //查询所有的借阅信息
        List<LibLoan> entities = libLoanMapper.selectLibLoanWithInfoList(libLoan,libBook,sysUser);

        //给每条借阅信息
        entities.stream().map(menu -> {
            LibLoan libLoan1 = loanStatusUtil.changeStatus(menu);
            return libLoan1;
        }).collect(Collectors.toList());
        return entities;
    }

    /**
     * 查询借阅信息列表(关联用户和书本信息)(删除的)
     *
     * @param libLoan 借阅信息
     * @param libBook 书本信息
     * @param sysUser 用户信息
     * @return
     */
    public List<LibLoan> selectLibLoanWithInfoListDel(LibLoan libLoan, LibBook libBook, SysUser sysUser)
    {
        //查询所有的借阅信息
        List<LibLoan> entities = libLoanMapper.selectLibLoanWithInfoListDel(libLoan,libBook,sysUser);

        //给每条借阅信息
        entities.stream().map(menu -> {
            LibLoan libLoan1 = loanStatusUtil.changeStatus(menu);
            return libLoan1;
        }).collect(Collectors.toList());
        return entities;
    }

    /**
     * 新增借阅信息
     * 
     * @param libLoan 借阅信息
     * @return 结果
     */
    @Override
    public int insertLibLoan(LibLoan libLoan)
    {
        return libLoanMapper.insertLibLoan(libLoan);
    }

    /**
     * 批量新增借阅信息
     *
     * @param libLoan 借阅信息
     * @return 结果
     */
    public int insertLibLoanBatch(String ids, LibLoan libLoan)
    {
        int result = 0;
        String[] bookIds = Convert.toStrArray(ids);
        Date today = DateUtils.truncate(DateUtils.getNowDate(), Calendar.DAY_OF_MONTH);//今日日期，格式yyyy-MM-dd

        //遍历ids进行批量添加
        for(int i = 0; i < bookIds.length; i++){
            LibLoan item = new LibLoan();
            LibBook libBook = new LibBook();

            item.setUserId(libLoan.getUserId());
            item.setBookId(Long.valueOf(bookIds[i]));
            item.setLoanTime(today);
            item.setPreReturnTime(libLoan.getPreReturnTime());

            libBook.setBookId(Long.valueOf(bookIds[i]));
            libBook.setLoanStatus(2L);

            //插入一条借阅信息
            result = libLoanMapper.insertLibLoan(item);
            //将该id对应的图书设为已借出
            libBookMapper.updateLibBook(libBook);

        }
        return result;
    }

    /**
     * 修改借阅信息
     * 
     * @param libLoan 借阅信息
     * @return 结果
     */
    @Override
    public int updateLibLoan(LibLoan libLoan)
    {
        //如果传过来的数据包含实际归还日期，而此id对应借阅信息没有归还日期，则把图书借阅状态置为未借出
        LibLoan info = libLoanMapper.selectLibLoanByLoanId(libLoan.getLoanId());
        if(info.getPraReturnTime() == null && libLoan.getPraReturnTime() != null)
        {
            LibBook libBook = new LibBook();
            libBook.setBookId(libLoan.getBookId());
            libBook.setLoanStatus(1L);
            libBookMapper.updateLibBook(libBook);
        }
        return libLoanMapper.updateLibLoan(libLoan);
    }

    /**
     * 批量修改借阅信息
     *
     * @return 结果
     */
    @Override
    public int updateLibLoanBatch(String ids){
        return libLoanMapper.updateLibLoanBatch(Convert.toStrArray(ids));
    }

    /**
     * 批量删除借阅信息
     * 
     * @param loanIds 需要删除的借阅信息主键
     * @return 结果
     */
    @Override
    public int deleteLibLoanByLoanIds(String loanIds)
    {
        return libLoanMapper.deleteLibLoanByLoanIds(Convert.toStrArray(loanIds));
    }

    /**
     * 删除借阅信息信息
     * 
     * @param loanId 借阅信息主键
     * @return 结果
     */
    @Override
    public int deleteLibLoanByLoanId(Long loanId)
    {
        return libLoanMapper.deleteLibLoanByLoanId(loanId);
    }

    /**
     * 批量删除借阅信息
     *
     * @param loanIds 需要删除的借阅信息主键
     * @return 结果
     */
    @Override
    public int deleteLibLoanByLoanIdsForever(String loanIds)
    {
        return libLoanMapper.deleteLibLoanByLoanIdsForever(Convert.toStrArray(loanIds));
    }

    /**
     * 删除借阅信息信息
     *
     * @param loanId 借阅信息主键
     * @return 结果
     */
    @Override
    public int deleteLibLoanByLoanIdForever(Long loanId)
    {
        return libLoanMapper.deleteLibLoanByLoanId(loanId);
    }
}
